package cn.edu.scau.cmi.ema.service.base;

import java.beans.PropertyEditorSupport;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;

@Component
public class CmiInputCurrentPaginationEditor extends  PropertyEditorSupport{
	
	private HttpServletRequest  request;
	
	public CmiInputCurrentPaginationEditor() {
		
	}
	
	public CmiInputCurrentPaginationEditor(HttpServletRequest  request) {
		this.request = request;
	}
	
	@Override 
	public void setAsText(String  inputCurrentPagination) {
		String actualCurrentPage = "";
		if(inputCurrentPagination.equals("")) {
			setValue(actualCurrentPage);
		}else {
			int currentPagination;
			try {
				// 判断数据是否都是数字类型 [0~9]
				currentPagination = Integer.parseInt((String) request.getParameter("inputCurrentPagination"));
			}catch(NumberFormatException e){
				actualCurrentPage = "1";
				setValue(actualCurrentPage);
				return ;
			}
			int totalPaginations = Integer.parseInt((String) request.getParameter("hiddenTotalPagination"));
			if(currentPagination >= 1 && currentPagination <= totalPaginations) {
				actualCurrentPage = "" + currentPagination;
			}else{
				actualCurrentPage = "1";
			}
			setValue(actualCurrentPage);
		}
	}
}